# -*- encoding: utf-8 -*-
"""
@File    : homework.py
@Time    : 2020/11/3 17:09
@Author  : lluyao
@Email   : lluyao61@163.com
@Software: PyCharm
"""
from IPy import IP
import os
def func():
    try:
        ip = input("请输入一个需要检测的网段/子网掩码：")
    except Exception as ex:
        print("error:", ex)
    else:
        ips = IP(ip)
        if len(ips) > 1:
            print("这是一个网段")
            print("网段", ips.net())
            print("子网掩码", ips.netmask())
        else:
            print("这是一个无效的格式请重新输入")
        f1 = open("not_use.txt", mode="a+", encoding="utf-8")
        f2 = open("used.txt", mode="a+", encoding="utf-8")
        for item in ips:
            cmd = f"ping {item} -n 1 -w 1"
            ret = os.system(cmd)
            if ret == 1:
                print(f"此{item}没被使用")
                f1.seek(0, 0)
                f1.write(f"{item}\n")
            else:
                print(f"此{item}已被使用")
                f2.seek(0, 0)
                f2.write(f"{item}\n")
        f1.close()
        f2.close()

func()
